/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

package com.chasekernan.hxnova.core.components;

import com.chasekernan.hxnova.core.components.ComponentSet;
import haxe.xml.Check;
import com.chasekernan.hxnova.core.minerals.MineralSet;
import com.chasekernan.hxnova.core.races.TechLevel;

/**
    Includes all beam weapons, including sappers/gattling guns.
**/
class BeamWeapon extends Component {
    
    //TODO: I've just ignored the column 80 limit here as these are just simple
    //methods that check to see if a value is with the limits. Maybe they should
    //be formatted correctly.
    
    public var power(default, setPower) : Int;
    private function setPower(i : Int) : Int {if(i <= 0) {throw "power must be greater than 0";} else {power = i;} return power;}
    
    public var range(default, setRange) : Int;
    private function setRange(i : Int) : Int {if(i < 0) {throw "range must be greater than or equal to 0";} else {range = i;} return range;}
    
    public var initiative(default, setInitiative) : Int;
    private function setInitiative(i : Int) : Int {if(i < 0) {throw "initiative must be greater than or equal to 0";} else {initiative = i;} return initiative;}
    
    public var isGattling : Bool;
    public var isSapper : Bool;
    
    public function new(name : String, ?power : Int, ?range : Int, 
            ?initiative: Int, ?isGattling : Bool, ?isSapper : Bool) {
        super(name);
        setPower(if(power == null) 1 else power);
        setRange(if(range == null) 0 else range);
        setInitiative(if (initiative == null) 0 else initiative);
        
        this.isGattling = if(isGattling == null) false else isGattling;
        this.isSapper = if(isSapper == null) false else isSapper;
    }
    
    public static function writeToXml(bw : BeamWeapon) : Xml {
        return Component.writeToXml(bw);
    }
    
    public static function readFromXml(xml : Xml) : BeamWeapon {
        try {
            var bw = new BeamWeapon("");
            //Check.checkNode(xml, bw.getRules());
            bw.generateFromComponentXml(xml, bw);
            
            return bw;
        } catch (e : Dynamic) {
            throw 'Bad xml file for beam weapon: ' + e;
        }
    }
}